# -*- coding: utf-8 -*-
"""
  Created by Intellij Pycharm.
  @Author: 苏苏少年
  @File: 表情.py
  @Time: 2022/4/30 15:19
"""

import requests
from lxml import etree

# 1. 找到表情包网站
url = 'http://www.bbsnet.com/biaoqingbao'

# 2. 想网站发送获取数据的请求
res = requests.get(url)

# print(res)
# <Response [200]>   200请求成功的编码

# print(res.text)

data = etree.HTML(res.text)
# print(data)

# 3. 过滤数据 关于表情包获取下来
# contains 集合的意思
img_list = data.xpath('//img[contains(@src,"http://")]')
# print(img_list)

# 4. 下载数据,保存到本地
for img in img_list:
    # 筛选表情包的图片
    url = img.xpath('./@src')[0]
    # 筛选表情包的名字
    name = img.xpath('./@alt')[0]
    # 发送下载数据的请求
    images = requests.get(url)
    # 保存本地
    with open('./表情包/' + name +'gif', 'wb')as f:
        f.write(images.content)
    print(name + '下载成功')
